package com.sea.physical.service.impl;

import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.ExchangeRate;
import com.sea.finance.service.ExchangeRateService;
import com.sea.physical.entity.CreateDespatchFreight;
import com.sea.physical.entity.CreatePlan;
import com.sea.physical.mapper.CreateDespatchFreightMapper;
import com.sea.physical.service.*;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.supply.entity.ProducePayment;
import com.sea.supply.entity.ProductPrice;
import com.sea.supply.entity.Supplier;
import com.sea.supply.service.SupplierService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class CreateDespatchFreightServiceImpl implements CreateDespatchFreightService {
    @Autowired
    private CreateDespatchFreightMapper createDespatchFreightMapper;

    @Autowired
    private ExchangeRateService exchangeRateService;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private SupplierService supplierService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(CreateDespatchFreight freight) {
        String createMoneyType = freight.getCreateMoneyType();
        ExchangeRate rate = exchangeRateService.findByRateName("CNY", createMoneyType);
        if(null==rate){
            throw new RuntimeException("暂未查询到币种汇率信息!【"+createMoneyType+"】");
        }else{
            freight.setCreateMoneyPercent(rate.getRatePercent());
        }
        createDespatchFreightMapper.insert(freight);
        return createDespatchFreightMapper.updateRealFreightTax(freight.getCreateId());
    }

    @Override
    public List<CreateDespatchFreight> findByCreateId(Integer createId) {
        return createDespatchFreightMapper.findByCreateId(createId);
    }

    @Override
    public int updateFreight(CreateDespatchFreight freight) {
        return createDespatchFreightMapper.updateFreight(freight);
    }

    @Override
    public int updateRealFreightTax(Integer createId) {
        return createDespatchFreightMapper.updateRealFreightTax(createId);
    }

    @Override
    public List<CreateDespatchFreight> findByCnd(CreateDespatchFreight createDespatchFreight) {
        return createDespatchFreightMapper.findByCnd(createDespatchFreight);
    }

    @Override
    public CreateDespatchFreight findById(Integer id) {
        return createDespatchFreightMapper.findById(id);
    }

    @Override
    public Integer usave(CreateDespatchFreight createDespatchFreight) {
        return createDespatchFreightMapper.update(createDespatchFreight);
    }

    @Override
    public Integer delete(Integer id) {
        Integer createId = findById(id).getCreateId();
        createDespatchFreightMapper.updateRealFreightTax(createId);
        return createDespatchFreightMapper.delete(id);
    }

    @Override
    public List<CreateDespatchFreight> findDetail() {
        return createDespatchFreightMapper.findDetail();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void pay(Integer[] payIds, String producePayTime, String produceFile) {
        for(Integer id:payIds){
            CreateDespatchFreight payment= findById(id);
            payment.setCreatePay(1);
            payment.setCreatePayFile(produceFile);
            payment.setCreatePayTime(producePayTime);
            usave(payment);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importDespatchFreight(InputStream inputStream, String fileName, String userRealName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<CreateDespatchFreight> list = BatchImportUtils.iteratorForDespathchFreight(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (CreateDespatchFreight despatchFreight : list) {
                    CreatePlan createPlan = createPlanService.findByCreateNo(despatchFreight.getCreatePlanNo());
                    if(null==createPlan){
                        throw new Exception("暂未查询到该柜号信息！【"+despatchFreight.getCreatePlanNo()+"】");
                    }
                    Supplier supplier = supplierService.findBySupplierName(despatchFreight.getCreateSupplier());
                    if(null==supplier){
                        throw new Exception("暂未查询到该供应商信息！【"+despatchFreight.getCreateSupplier()+"】");
                    }

                    despatchFreight.setCreateId(createPlan.getId());
                    save(despatchFreight);
                }
            }
        }
    }
}
